﻿using HardcoreGame.Content.Items.Weapons.Ranged.Guns;
using Microsoft.Xna.Framework;
using System;
using Terraria;
using Terraria.Audio;
using Terraria.ID;
using Terraria.ModLoader;

namespace HardcoreGame.Common.Systems
{
    public class PhoenixBlasterSystem : ModSystem
    {
        public static bool IsTransforming = false;
        public static int TransformTimer = 0;
        public static int TransformItemIndex = -1;
        private const int PhoenixBlasterItemID = ItemID.PhoenixBlaster;
        public override void PostUpdateEverything()
        {
            if (Main.LocalPlayer.ZoneSnow && !IsTransforming)
            {
                CheckForTransformation();
            }
            if (IsTransforming)
            {
                HandleTransformation();
            }
        }

        private void CheckForTransformation()
        {
            for (int i = 0; i < Main.maxItems; i++)
            {
                Item item = Main.item[i];
                if (item.active && item.type == PhoenixBlasterItemID && !item.favorited && item.stack > 0)
                {
                    StartTransformation(i);
                    break;
                }
            }
        }

        private static void StartTransformation(int itemIndex)
        {
            IsTransforming = true;
            TransformTimer = 0;
            TransformItemIndex = itemIndex;
            SoundEngine.PlaySound(SoundID.Item27, Main.item[itemIndex].Center);
        }

        private void HandleTransformation()
        {
            TransformTimer++;
            Item item = Main.item[TransformItemIndex];

            if (!item.active || item.type != PhoenixBlasterItemID)
            {
                ResetTransformation();
                return;
            }
            if (TransformTimer < 60)
            {
                if (TransformTimer % 3 == 0)
                {
                    for (int i = 0; i < 5; i++)
                    {
                        Vector2 spawnPos = item.Center + new Vector2(
                            Main.rand.NextFloat(-30, 30),
                            Main.rand.NextFloat(-30, 30)
                        );

                        Dust dust = Dust.NewDustPerfect(spawnPos, DustID.IceTorch,
                            Vector2.Normalize(item.Center - spawnPos) * Main.rand.NextFloat(1, 3),
                            100, default, 1.5f);

                        dust.noGravity = true;
                    }
                }
            }
            else if (TransformTimer < 180)
            {
                if (TransformTimer % 2 == 0)
                {
                    for (int i = 0; i < 3; i++)
                    {
                        Vector2 spawnPos = item.Center + new Vector2(
                            Main.rand.NextFloat(-20, 20),
                            Main.rand.NextFloat(-20, 20)
                        );

                        Dust dust = Dust.NewDustPerfect(spawnPos, DustID.IceTorch,
                            Vector2.Normalize(item.Center - spawnPos) * Main.rand.NextFloat(1, 2),
                            100, default, 1.5f);

                        dust.noGravity = true;
                    }
                }
                item.alpha = (int)(Math.Sin(TransformTimer * 0.1f) * 60) + 100;
                item.position.Y -= 0.1f;
            }
            else if (TransformTimer < 240)
            {
                for (int i = 0; i < 4; i++)
                {
                    Vector2 velocity = new Vector2(
                        Main.rand.NextFloat(-2, 2),
                        Main.rand.NextFloat(-2, 2)
                    );

                    Dust dust = Dust.NewDustPerfect(item.Center, DustID.GemSapphire, velocity, 100, default, 2f);
                    dust.noGravity = true;
                }
                item.alpha = (int)(Math.Sin(TransformTimer * 0.2f) * 100) + 50;
                item.position.Y -= 0.2f;
                if (TransformTimer == 180)
                {
                    SoundEngine.PlaySound(SoundID.Item28, item.Center);
                }
            }
            else
            {
                int oldPrefix = item.prefix;
                int oldStack = item.stack;
                Vector2 oldPosition = item.position;
                int newItemIndex = Item.NewItem(
                    Entity.GetSource_NaturalSpawn(),
                    (int)oldPosition.X,
                    (int)oldPosition.Y,
                    item.width,
                    item.height,
                    ModContent.ItemType<FrostBurstBlaster>(),
                    oldStack
                );
                Main.item[newItemIndex].newAndShiny = true;
                Main.item[newItemIndex].noGrabDelay = 30;
                Main.item[newItemIndex].prefix = oldPrefix;
                for (int i = 0; i < 15; i++)
                {
                    Vector2 velocity = new(
                        Main.rand.NextFloat(-3, 3),
                        Main.rand.NextFloat(-3, 3)
                    );

                    Dust dust = Dust.NewDustPerfect(Main.item[newItemIndex].Center, DustID.IceTorch, velocity, 100, default, 2f);
                    dust.noGravity = true;
                }
                SoundEngine.PlaySound(SoundID.Item4, Main.item[newItemIndex].Center);
                item.TurnToAir();
                ResetTransformation();
            }
        }

        private static void ResetTransformation()
        {
            IsTransforming = false;
            TransformTimer = 0;
            TransformItemIndex = -1;
        }
    }
    public class PhoenixBlasterSystem1 : ModSystem
    {
        public static bool IsTransforming = false;
        public static int TransformTimer = 0;
        public static int TransformItemIndex = -1;

        public override void PostUpdateEverything()
        {
            if (Main.LocalPlayer.ZoneUnderworldHeight && !IsTransforming)
            {
                CheckForTransformation();
            }
            if (IsTransforming)
            {
                HandleTransformation();
            }
        }

        private static void CheckForTransformation()
        {
            for (int i = 0; i < Main.maxItems; i++)
            {
                Item item = Main.item[i];
                if (item.active && item.type == ModContent.ItemType<FrostBurstBlaster>() && !item.favorited && item.stack > 0)
                {
                    StartTransformation(i);
                    break;
                }
            }
        }

        private static void StartTransformation(int itemIndex)
        {
            IsTransforming = true;
            TransformTimer = 0;
            TransformItemIndex = itemIndex;
            SoundEngine.PlaySound(SoundID.Item27, Main.item[itemIndex].Center);
        }

        private static void HandleTransformation()
        {
            TransformTimer++;
            Item item = Main.item[TransformItemIndex];

            if (!item.active || item.type != ModContent.ItemType<FrostBurstBlaster>())
            {
                ResetTransformation();
                return;
            }
            if (TransformTimer < 60)
            {
                if (TransformTimer % 3 == 0)
                {
                    for (int i = 0; i < 5; i++)
                    {
                        Vector2 spawnPos = item.Center + new Vector2(
                            Main.rand.NextFloat(-30, 30),
                            Main.rand.NextFloat(-30, 30)
                        );

                        Dust dust = Dust.NewDustPerfect(spawnPos, DustID.Torch,
                            Vector2.Normalize(item.Center - spawnPos) * Main.rand.NextFloat(1, 3),
                            100, default, 1.5f);

                        dust.noGravity = true;
                    }
                }
            }
            else if (TransformTimer < 180)
            {
                if (TransformTimer % 2 == 0)
                {
                    for (int i = 0; i < 3; i++)
                    {
                        Vector2 spawnPos = item.Center + new Vector2(
                            Main.rand.NextFloat(-20, 20),
                            Main.rand.NextFloat(-20, 20)
                        );

                        Dust dust = Dust.NewDustPerfect(spawnPos, DustID.Torch,
                            Vector2.Normalize(item.Center - spawnPos) * Main.rand.NextFloat(1, 2),
                            100, default, 1.5f);

                        dust.noGravity = true;
                    }
                }
                item.alpha = (int)(Math.Sin(TransformTimer * 0.1f) * 60) + 100;
                item.position.Y -= 0.1f;
            }
            else if (TransformTimer < 240)
            {
                for (int i = 0; i < 4; i++)
                {
                    Vector2 velocity = new Vector2(
                        Main.rand.NextFloat(-2, 2),
                        Main.rand.NextFloat(-2, 2)
                    );

                    Dust dust = Dust.NewDustPerfect(item.Center, DustID.Lava, velocity, 100, default, 2f);
                    dust.noGravity = true;
                }
                item.alpha = (int)(Math.Sin(TransformTimer * 0.2f) * 100) + 50;
                item.position.Y -= 0.2f;
                if (TransformTimer == 180)
                {
                    SoundEngine.PlaySound(SoundID.Item28, item.Center);
                }
            }
            else
            {
                int oldPrefix = item.prefix;
                int oldStack = item.stack;
                Vector2 oldPosition = item.position;
                int newItemIndex = Item.NewItem(
                    Entity.GetSource_NaturalSpawn(),
                    (int)oldPosition.X,
                    (int)oldPosition.Y,
                    item.width,
                    item.height,
                    ItemID.PhoenixBlaster,
                    oldStack
                );
                Main.item[newItemIndex].newAndShiny = true;
                Main.item[newItemIndex].noGrabDelay = 30;
                Main.item[newItemIndex].prefix = oldPrefix;
                for (int i = 0; i < 15; i++)
                {
                    Vector2 velocity = new Vector2(
                        Main.rand.NextFloat(-3, 3),
                        Main.rand.NextFloat(-3, 3)
                    );

                    Dust dust = Dust.NewDustPerfect(Main.item[newItemIndex].Center, DustID.Torch, velocity, 100, default, 2f);
                    dust.noGravity = true;
                }
                SoundEngine.PlaySound(SoundID.Item4, Main.item[newItemIndex].Center);
                item.TurnToAir();
                ResetTransformation();
            }
        }

        private static void ResetTransformation()
        {
            IsTransforming = false;
            TransformTimer = 0;
            TransformItemIndex = -1;
        }
    }
}
